package com.ces.knacky.controller.tools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ces.knacky.framework.controller.BaseController;
import com.ces.knacky.services.AuctionService;

/**
 * 
 * @author shivangi_walvekar
 * 
 */
// TODO - reomve sysouts
@Controller
public class SaveBidControlller extends BaseController {
	
	@Autowired
	private AuctionService auctionService;	
	private Float bidPrice;

	@RequestMapping("/saveBid.htm")
	public String execute(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String itemId = (String) session.getAttribute("itemId");
		if(itemId == null || itemId.isEmpty()){
			// TODO - log and throw application specific exception
			System.out.println("found null itemId from session");
		}		
//		String selectedAuction = (String) session.getAttribute("selectedAuction");
		Integer selectedAuctionID = (Integer) session.getAttribute("selectedAuctionId");
		auctionService.placeBid(Integer.parseInt(itemId), selectedAuctionID, getBidPrice());
		return "saveBid";
	}

	/**
	 * @return the auctionService
	 */
	public AuctionService getAuctionService() {
		return auctionService;
	}

	/**
	 * @param auctionService - the AuctionService to set
	 */
	public void setAuctionService(AuctionService auctionService) {
		this.auctionService = auctionService;
	}

	/**
	 * @return bidPrice
	 */
	public Float getBidPrice() {
		return bidPrice;
	}

	/**
	 * @param bidPrice
	 */
	@ModelAttribute("bidPrice")
	public void setBidPrice(Float bidPrice) {
		this.bidPrice = bidPrice;
	}
}
